Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exec_app_context corrupting stack #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hatoo
Copy link

@hatoo hatoo commented Nov 13, 2024

A context switch will corrupt an app's stack.

I think this PR fix it.

I've found it by searching for an issue that

#![no_std]
#![no_main]

extern crate alloc;

use alloc::vec::Vec;
#[cfg_attr(target_os = "linux", no_main)]
use noli::prelude::*;
entry_point!(main);

fn main() -> u64 {
    let mut v: Vec<usize> = Vec::new();
    v.reserve(1); // This allocation is no problem but this increases segment size significantly because it requires noli's ALLOCATOR
    0
}

This app caused a crash when I ran it a few times in a row.

And I've concluded that a context switch accidentally overwrites ALLOCATOR(OS side)'s table's headers by 0 and header's linked list is cat because (None::<Option<Box<Header>>> is 0)

Then OS can't allocate a region for exec the app -> crash

hikalium added a commit that referenced this pull request Nov 13, 2024
@hikalium
Copy link
Owner

Thanks for the report! I created a repro branch and reproduced the issue with the following steps:

expected behavior: bug0 runs 1000 times without an error
actual behavior: it fails with the following log messages:

> bug0
[INFO]  os/src/cmd.rs:58 :  Executing cmd: ["bug0"]
[INFO]  os/src/cmd.rs:153:  try 0
[INFO]  os/src/cmd.rs:156:  Ok(0)
// snip
[INFO]  os/src/cmd.rs:153:  try 157
[PANIC] start
[PANIC] RIP = 0x000000003E20593D
[PANIC] RSP = 0x000000003E1DCD58
[PANIC] RBP = 0x000000003E1DCDD8
[PANIC] *RBP = 0x0000000000000000
[PANIC] dump_stack() = 0x000000003E205910
// snip
00000000: 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000010: 00 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 |................|
00000020: 01 00 00 00 00 00 00 00 20 00 00 00 0C 00 00 00 |........ .......|
00000030: 03 CE 1D 3E 00 00 00 00 88 CD 1D 3E 00 00 00 00 |...>.......>....|
[PANIC] PanicInfo { payload: Any { .. }, message: Some(assertion failed: self.canary == CANARY_SIGNATURE), location: Location { file: "os/src/allocator.rs", line: 112, col: 9 }, can_unwind: true, force_no_backtrace: false }

However, although the branch above contains the suggested fix, the error is still observed... let me look into it!

@hatoo
Copy link
Author

hatoo commented Nov 13, 2024

I think OS error is because of memory shortage.
Everytime when exec an app, OS doesn't dealloc used app's region and stack. it leaks.

I got ALLOCATOR's dump after execution failed
b.txt
The headers looks good but it seems that there are no spaces to fit app'a region

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants